package com.kla.power.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.kla.power.entity.MenuEntity;
import com.kla.power.entity.RoleEntity;
import com.kla.power.entity.RoleMenuEntity;
import com.kla.power.mapper.RoleMapper;
import com.kla.power.mapper.RoleMenuMapper;
import com.kla.power.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kla.power.util.SnowflakeIdWorker;
import com.kla.power.util.StateEnum;
import com.kla.power.util.StateException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author WuZH
 * @since 2024-06-02
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, RoleEntity> implements IRoleService {


    @Resource
    private RoleMapper roleMapper;
    @Resource
    private RoleMenuMapper roleMenuMapper;

    @Override
    public List<RoleEntity> queryList (RoleEntity roleEntity) {
        if ( roleEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        roleEntity.setPage ( ( roleEntity.getPage () - 1 ) * roleEntity.getLimit () );
        List<RoleEntity> roleEntities = roleMapper.queryList ( roleEntity );
        return roleEntities;
    }

    @Override
    public Integer queryCount (RoleEntity roleEntity) {
        if ( roleEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        Integer count = roleMapper.queryCount ( roleEntity );
        return count;
    }

    /**
     * 编辑的回显，带角色信息以及已选择的权限列表
     * @param roleEntity
     * @return
     */
    @Override
    public RoleEntity querySingle (RoleEntity roleEntity) {
        if ( roleEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        if ( StringUtils.isBlank ( roleEntity.getId () ) ) {
            throw new StateException ( StateEnum.PRIMARY_NULL_EXCE );
        }
        RoleEntity role = baseMapper.selectById ( roleEntity );
        // 获取已选择的权限列表
        RoleMenuEntity roleMenuEntity = new RoleMenuEntity ();
        roleMenuEntity.setRoleId ( role.getId () );
        List<MenuEntity> menuEntities = roleMenuMapper.queryList ( roleMenuEntity );
        role.setMenuEntities ( menuEntities );
        return role;
    }

    /**
     * 添加一个新角色的同时，携带菜单权限集合
     * @param roleEntity
     * @return
     */
    @Transactional ( rollbackFor = Exception.class, propagation = Propagation.REQUIRED )
    @Override
    public RoleEntity saveSingle (RoleEntity roleEntity) {
        if ( roleEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        if ( roleEntity.getId () == null ) {
            throw new StateException ( StateEnum.PRIMARY_NULL_EXCE );
        }
        roleEntity.setCreateTime ( new Date () );
        int insert = baseMapper.insert ( roleEntity );
        if ( insert == 0 ) {
            throw new StateException ( StateEnum.INSERT_EXCE );
        }
        // 绑定角色权限关系
        for ( MenuEntity menuEntity : roleEntity.getMenuEntities () ) {
            RoleMenuEntity roleMenu = new RoleMenuEntity ();
            roleMenu.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
            roleMenu.setRoleId ( roleEntity.getId () );
            roleMenu.setMenuId ( menuEntity.getId () );
            roleMenu.setCreateTime ( new Date () );
            roleMenuMapper.saveSingle ( roleMenu );
        }
        return roleEntity;
    }

    @Transactional ( rollbackFor = Exception.class, propagation = Propagation.REQUIRED )
    @Override
    public RoleEntity updateSingle (RoleEntity roleEntity) {
        if ( roleEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        if ( StringUtils.isBlank ( roleEntity.getId () ) ) {
            throw new StateException ( StateEnum.PRIMARY_NULL_EXCE );
        }
        UpdateWrapper<RoleEntity> updateWrapper = new UpdateWrapper<> ();
        if ( StringUtils.isNotBlank ( roleEntity.getRoleName () ) ) {
            updateWrapper.set ( "roleName", roleEntity.getRoleName () );
        }
        if ( StringUtils.isNotBlank ( roleEntity.getRoleDesc () ) ) {
            updateWrapper.set ( "roleDesc", roleEntity.getRoleDesc () );
        }
        if ( roleEntity.getIsUsed () != null ) {
            updateWrapper.set ( "isUsed", roleEntity.getIsUsed () );
        }
        updateWrapper.eq ( "id", roleEntity.getId () );
        roleEntity.setChangeTime ( new Date () );
        int update = baseMapper.update ( null, updateWrapper );
        if ( update == 0 ) {
            throw new StateException ( StateEnum.UPDATE_EXCE );
        }
        // 修改该角色对应的权限，删除原来的权限，新增权限
        if ( roleEntity.getMenuEntities ().size () > 0 ) {
            RoleMenuEntity roleMenuEntity = new RoleMenuEntity ();
            roleMenuEntity.setRoleId ( roleEntity.getId () );
            roleMenuMapper.deleteBatch ( roleMenuEntity );
            for ( MenuEntity menuEntity : roleEntity.getMenuEntities () ) {
                RoleMenuEntity roleMenu = new RoleMenuEntity ();
                roleMenu.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
                roleMenu.setRoleId ( roleEntity.getId () );
                roleMenu.setMenuId ( menuEntity.getId () );
                roleMenu.setCreateTime ( new Date () );
                roleMenuMapper.saveSingle ( roleMenu );
            }
        }
        return roleEntity;
    }

}

